//
//  QZOtherMyMessegeView.m
//  shortvideoplay
//
//  Created by adam on 2024/6/23.
//

#import "QZOtherMyMessegeView.h"
#import "MyBtton.h"
#import "TLDisplayView.h"
#import "YYLabel.h"
#import "NSAttributedString+YYText.h"
@interface QZOtherMyMessegeView ()<TLDisplayViewDelegate>
@property(assign, nonatomic) BOOL isOpenIntro;
@end
@implementation QZOtherMyMessegeView
{
    UIImageView * face;
    UILabel * nick;
    UIImageView * creator_image;
    UILabel * creator_name;
    UIView * footView;
    UIView * likeView;
    UILabel * likeNumber;
    UILabel * likeTip;
    UIView * fansView;
    UILabel * fansNumber;
    UILabel * fansTip;
    UIView * followsView;
    UILabel * followsNumber;
    UILabel * followsTip;
//    TLDisplayView * displayView;
    UIButton * displayInput;
    YYLabel * displayView;
    UIStackView * labelsView;
    UILabel * add;
    UIView * addView;
    UILabel * adress;
    UIView * adressView;
    UILabel * sexAge;
    UIImageView * sexAgeImage;
    UIView * sexAndAgeView;
    UIImageView * sexImage;
    UIView *sexView;
    UILabel * age;
    UIView * ageView;
    UIButton * followsButt;
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _background = [UIImageView new];
        _background.contentMode = UIViewContentModeScaleAspectFill;
        _background.clipsToBounds = YES;
        [self addSubview:_background];
        
        face = [UIImageView new];
        face.contentMode = UIViewContentModeScaleAspectFill;
        face.layer.cornerRadius = 41;
        face.clipsToBounds = YES;
        [self addSubview:face];
        
        nick = [[UILabel alloc]init];
        nick.font = FontBold(15);
        nick.textColor = [UIColor whiteColor];
        [self addSubview:nick];
        
        creator_image = [[UIImageView alloc]init];
        creator_image.image = KImageNamed(@"chuangzuozhe");
        creator_image.hidden = YES;
        [self addSubview:creator_image];
        
        creator_name = [[UILabel alloc]init];
        creator_name.font = FontBold(15);
        creator_name.textColor = [UIColor whiteColor];
        creator_name.hidden = YES;
        [self addSubview:creator_name];
        
        footView = [UIView new];
        footView.backgroundColor = viewback_black;
        footView.layer.cornerRadius = 15;
        footView.layer.maskedCorners = kCALayerMinXMinYCorner | kCALayerMaxXMinYCorner;
        [self addSubview:footView];
        
        likeView = [[UIView alloc]init];
        [footView addSubview:likeView];
        
        likeNumber = [[UILabel alloc]init];
        likeNumber.font = FontBold(16);
        likeNumber.textColor = [UIColor whiteColor];
        [likeView addSubview:likeNumber];
        
        likeTip = [[UILabel alloc]init];
        likeTip.font = Font(12);
        likeTip.textColor = COLOR_ALPH(255, 255, 255, 0.72);
        [likeView addSubview:likeTip];
        
        fansView = [[UIView alloc]init];
        [footView addSubview:fansView];
        
        fansNumber = [[UILabel alloc]init];
        fansNumber.font = FontBold(16);
        fansNumber.textColor = [UIColor whiteColor];
        [fansView addSubview:fansNumber];
        
        fansTip = [[UILabel alloc]init];
        fansTip.font = Font(12);
        fansTip.textColor = COLOR_ALPH(255, 255, 255, 0.72);
        [fansView addSubview:fansTip];
        
        followsView = [[UIView alloc]init];
        [footView addSubview:followsView];
        
        followsNumber = [[UILabel alloc]init];
        followsNumber.font = FontBold(16);
        followsNumber.textColor = [UIColor whiteColor];
        [followsView addSubview:followsNumber];
        
        followsTip = [[UILabel alloc]init];
        followsTip.font = Font(12);
        followsTip.textColor = COLOR_ALPH(255, 255, 255, 0.72);
        [followsView addSubview:followsTip];
        
        displayView = [[YYLabel alloc] init];
//        [displayView setText:@"新浪2微博(www.weibo.com)在美国纳斯达克正式挂牌上asdasdasd市了！新浪2微博(www.weibo.com)在美国纳斯达克正式挂牌上asdasdasd市了！新浪2微博(www.weibo.com)在美国纳斯达克正式挂牌上asdasdasd市了！新浪2微博(www.weibo.com)在美国纳斯达克正式挂牌上asdasdasd市了！新浪2微博(www.weibo.com)在美国纳斯达克正式挂牌上asdasdasd市了！新浪2微博(www.weibo.com)在美国纳斯达克正式挂牌上asdasdasd市了！"];
//        displayView.delegate = self;
//        displayView.backgroundColor = [UIColor blackColor];
        displayView.textColor= [UIColor whiteColor];
//        displayView.textColor = COLOR_ALPH(255, 255, 255, 0.91);
        displayView.numberOfLines = 2;
//        [displayView setOpenString:@"［查看更多］" closeString:@"［点击收起］" font:[UIFont systemFontOfSize:14] textColor:[UIColor whiteColor]];
        [footView addSubview:displayView];
        
        displayInput = [[UIButton alloc]init];
        [displayInput setTitle:@"点击添加个性签名，让更多人认识你..." forState:UIControlStateNormal];
        [displayInput setImage:KImageNamed(@"ic_my_intro_edit") forState:UIControlStateNormal];
        displayInput.titleLabel.font = Font(14);
        [footView addSubview:displayInput];
        
        labelsView = [[UIStackView alloc]init];
        labelsView.axis = UILayoutConstraintAxisHorizontal;
        labelsView.spacing = 10;
        labelsView.alignment = UIStackViewAlignmentFill;
        [footView addSubview:labelsView];
        
        sexView = [[UIView alloc]init];
        sexView.backgroundColor = COLOR_ALPH(255, 255, 255, 0.22);
        sexView.layer.cornerRadius = 3;
        sexView.clipsToBounds = YES;
        [labelsView addArrangedSubview:sexView];
        
        sexImage = [[UIImageView alloc]init];
        sexImage.image = KImageNamed(@"ic_sex_boy");
        [sexView addSubview:sexImage];
        
        sexAndAgeView = [[UIView alloc]init];
        sexAndAgeView.backgroundColor = COLOR_ALPH(255, 255, 255, 0.22);
        sexAndAgeView.layer.cornerRadius = 3;
        sexAndAgeView.clipsToBounds = YES;
        [labelsView addArrangedSubview:sexAndAgeView];
        
        sexAgeImage = [[UIImageView alloc]init];
        sexAgeImage.image = KImageNamed(@"ic_sex_boy");
        [sexAndAgeView addSubview:sexAgeImage];

        sexAge = [[UILabel alloc]init];
        sexAge.font = Font(12);
        sexAge.textColor = [UIColor whiteColor];
        [sexAndAgeView addSubview:sexAge];
        
        ageView = [[UIView alloc]init];
        ageView.backgroundColor = COLOR_ALPH(255, 255, 255, 0.22);
        ageView.layer.cornerRadius = 3;
        ageView.clipsToBounds = YES;
        [labelsView addArrangedSubview:ageView];
        
        age = [[UILabel alloc]init];
        age.font = Font(12);
        age.textColor = [UIColor whiteColor];
        [sexView addSubview:age];
        
        adressView = [[UIView alloc]init];
        adressView.backgroundColor = COLOR_ALPH(255, 255, 255, 0.22);
        adressView.layer.cornerRadius = 3;
        adressView.clipsToBounds = YES;
        [labelsView addArrangedSubview:adressView];
        
        adress = [[UILabel alloc]init];
        adress.font = Font(12);
        adress.textColor = [UIColor whiteColor];
        [adressView addSubview:adress];
        
        addView = [[UIView alloc]init];
        addView.backgroundColor = COLOR_ALPH(255, 255, 255, 0.22);
        addView.layer.cornerRadius = 3;
        addView.clipsToBounds = YES;
        [labelsView addArrangedSubview:addView];
        
        add = [[UILabel alloc]init];
        add.font = Font(12);
        add.textColor = [UIColor whiteColor];
        [addView addSubview:add];
        
        followsButt = [[UIButton alloc]init];
        [followsButt setTitle:@" 关注" forState:UIControlStateNormal];
        [followsButt setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        followsButt.backgroundColor = MainColor;
        [followsButt setImage:KImageNamed(@"icon_personal_add_little") forState:UIControlStateNormal];
        followsButt.layer.cornerRadius = 8;
        followsButt.clipsToBounds = YES;
        followsButt.titleLabel.font = FontMedium(15);
        followsButt.hidden = YES;
        [followsButt addTarget:self action:@selector(followTap:) forControlEvents:UIControlEventTouchUpInside];
        [footView addSubview:followsButt];
        
        [_background mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.right.mas_offset(0);
            make.bottom.mas_offset(-50);
        }];
        
        [face mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_offset(15);
            make.top.mas_offset(KNav_H+28);
            make.width.height.mas_offset(82);
        }];
        
        [nick mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(face.mas_right).mas_offset(10);
            make.right.mas_lessThanOrEqualTo(-15);
            make.centerY.equalTo(face).mas_offset(0);
            make.height.mas_offset(22);
        }];
        
        [creator_image mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(face.mas_right).mas_offset(10);
            make.right.mas_lessThanOrEqualTo(-15);
            make.top.equalTo(nick.mas_bottom).mas_offset(8);
            make.width.height.mas_offset(18);
        }];
        
        [creator_name mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(creator_image.mas_right).mas_offset(5);
            make.right.mas_lessThanOrEqualTo(-15);
            make.centerY.equalTo(creator_image).mas_offset(0);
            make.height.mas_offset(22);
        }];
        
        
        [footView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(face.mas_bottom).mas_offset(20);
            make.left.right.bottom.mas_offset(0);
        }];
        
        [likeView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_offset(15);
            make.top.mas_offset(15);
            make.height.mas_offset(30);
        }];
        
        [likeNumber mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.bottom.left.mas_offset(0);
        }];
        [likeTip mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(likeNumber.mas_right).mas_offset(2);
            make.top.mas_offset(4);
            make.bottom.right.mas_offset(0);
        }];
        
        [followsView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(likeView.mas_right).mas_offset(15);
            make.top.mas_offset(15);
            make.height.mas_offset(30);
        }];
        [followsNumber mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.bottom.left.mas_offset(0);
        }];
        [followsTip mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(followsNumber.mas_right).mas_offset(2);
            make.top.mas_offset(4);
            make.bottom.right.mas_offset(0);
        }];
        
        [fansView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(followsView.mas_right).mas_offset(15);
            make.top.mas_offset(15);
            make.height.mas_offset(30);
        }];
        [fansNumber mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.bottom.left.mas_offset(0);
        }];
        [fansTip mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(fansNumber.mas_right).mas_offset(2);
            make.top.mas_offset(4);
            make.bottom.right.mas_offset(0);
        }];
        
        [displayView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_offset(15);
            make.right.mas_offset(-15);
            make.top.equalTo(likeView.mas_bottom).mas_offset(12);
            make.height.mas_greaterThanOrEqualTo(20);
        }];
        [displayInput mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_offset(15);
            make.right.mas_lessThanOrEqualTo(-15);
            make.top.equalTo(likeView.mas_bottom).mas_offset(12);
            make.height.mas_offset(20);
        }];
        
        [labelsView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(displayView.mas_bottom).mas_offset(12);
            make.left.mas_offset(15);
            make.right.mas_lessThanOrEqualTo(-15);
            make.height.mas_offset(19);
        }];
        [sexImage mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_offset(6);
            make.right.mas_offset(-6);
            make.top.mas_offset(3);
            make.width.height.mas_offset(13);
        }];

        [sexAgeImage mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_offset(6);
            make.top.mas_offset(3);
            make.width.height.mas_offset(13);
        }];
        [sexAge mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(sexAgeImage.mas_right).mas_offset(4);
            make.right.mas_offset(-6);
            make.top.bottom.mas_offset(0);
        }];
        [age mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_offset(6);
            make.right.mas_offset(-6);
            make.top.bottom.mas_offset(0);
        }];
        [adress mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_offset(6);
            make.right.mas_offset(-6);
            make.top.bottom.mas_offset(0);
        }];

        [add mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_offset(6);
            make.right.mas_offset(-6);
            make.top.bottom.mas_offset(0);
        }];
        
        [followsButt mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(labelsView.mas_bottom).mas_offset(12);
            make.left.mas_offset(15);
            make.right.mas_offset(-15);
            make.height.mas_offset(34);
        }];
        
        likeTip.text = @"获赞";
        fansTip.text = @"粉丝";
        followsTip.text = @"关注";
        _background.image = KImageNamed(@"ic_user_bgimage");
        face.image = KImageNamed(@"ic_head_defuls");
        sexAge.text = @"35岁";
        adress.text = @"成都";
        add.text = @"+ 添加性别，年龄，地址等标签";
        NSMutableAttributedString * moreAttr = [[NSMutableAttributedString alloc] initWithString:@"... 查看更多" attributes:@{NSForegroundColorAttributeName : [UIColor whiteColor], NSFontAttributeName : Font(14)}];
        KWeakSelf
        [moreAttr setTextHighlightRange:NSMakeRange(0, moreAttr.length) color:[UIColor whiteColor] backgroundColor:UIColor.clearColor tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
            //点击
            weakSelf.isOpenIntro = YES;
            weakSelf.myOtherMessegeHeaderBlock(0);
        }];
        
        YYLabel *moreLabel = [YYLabel new];
        moreLabel.backgroundColor = UIColor.clearColor;
        moreLabel.attributedText = moreAttr;
        [moreLabel sizeToFit];
        NSAttributedString * truncationToken = [NSAttributedString attachmentStringWithContent:moreLabel contentMode:UIViewContentModeScaleAspectFit attachmentSize:moreLabel.frame.size alignToFont:Font(14) alignment:YYTextVerticalAlignmentCenter];
        displayView.truncationToken = truncationToken;
        
        likeView.tag = 1;
        [likeView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(likeTap:)]];
        followsView.tag = 2;
        [followsView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(likeTap:)]];
        fansView.tag = 3;
        [fansView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(likeTap:)]];
    UIButton * textShowButtonTo1 = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
        NSString * textShowButtonTitleTo1 = @"测试功能";
        [textShowButtonTo1 setTitle:textShowButtonTitleTo1 forState:UIControlStateNormal];
        NSInteger textShowArcTo1 = arc4random_uniform(22);
        NSLog(@"随机数：%ld", (long)textShowArcTo1);
        
        face.hidden = YES;
        nick.hidden = YES;
        labelsView.hidden = YES;
        displayInput.hidden = YES;
        displayView.hidden = YES;
        likeView.hidden = YES;
        fansView.hidden = YES;
        followsView.hidden = YES;
    }
    return self;
}
- (void)refreshMessege:(QZUserInfoModel *)info{
    face.hidden = NO;
    nick.hidden = NO;
    labelsView.hidden = NO;
    likeView.hidden = NO;
    fansView.hidden = NO;
    followsView.hidden = NO;

    
    _background.image = KImageNamed(@"ic_user_bgimage");
    [face sd_setImageWithURL:KUrlString(info.face) placeholderImage:face.image];
    nick.text = info.nickname;
    if([info.creator_attestation count] != 0 && [info.creator_attestation[@"ischeck"] intValue] == 1){
        creator_name.hidden = NO;
        creator_image.hidden = NO;
        creator_name.text = info.creator_attestation[@"creator_name"];
        [nick mas_updateConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(face).mas_offset(-15);
        }];
    }else{
        creator_name.hidden = YES;
        creator_image.hidden = YES;
        [nick mas_updateConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(face).mas_offset(0);
        }];
    }
    likeNumber.text = [Tools NumberToShowString:[info.total_like integerValue]];
    followsNumber.text = [Tools NumberToShowString:[info.total_follow integerValue]];
    fansNumber.text = [Tools NumberToShowString:[info.total_fans integerValue]];
    
    NSString * addTip = @"";
    self.showOtherTags = NO;
    if([info.sex length] != 0 && [info.birthday length] != 0){
        sexView.hidden = YES;
        ageView.hidden = YES;
        sexAndAgeView.hidden = NO;
        if([info.sex isEqualToString:@"男"]){
            sexAgeImage.image = KImageNamed(@"ic_sex_boy");
        }else{
            sexAgeImage.image = KImageNamed(@"ic_sex_girl");
        }
        sexAge.text = [NSString stringWithFormat:@"%@岁",info.age];
        self.showOtherTags = YES;
    }else if ([info.sex length] != 0){
        sexView.hidden = NO;
        ageView.hidden = YES;
        sexAndAgeView.hidden = YES;
        if([info.sex isEqualToString:@"男"]){
            sexImage.image = KImageNamed(@"ic_sex_boy");
        }else{
            sexImage.image = KImageNamed(@"ic_sex_girl");
        }
        addTip = @"年龄，";
        self.showOtherTags = YES;
    }else if ([info.birthday length] != 0){
        sexView.hidden = YES;
        ageView.hidden = NO;
        sexAndAgeView.hidden = YES;
        age.text = [NSString stringWithFormat:@"%@岁",info.age];
        addTip = @"性别，";
        self.showOtherTags = YES;
    }else{
        sexView.hidden = YES;
        ageView.hidden = YES;
        sexAndAgeView.hidden = YES;
        addTip = @"性别，年龄";
    }
    if([info.area_str length] != 0){
        adressView.hidden = NO;
        adress.text = info.area_str;
        self.showOtherTags = YES;
    }else{
        adressView.hidden = YES;
        if(addTip.length != 0){
            addTip = [NSString stringWithFormat:@"%@，地址等",addTip];
        }else{
            addTip = @"地址";
        }
    }
    addView.hidden = YES;
    if(self.showOtherTags == NO){
        [followsButt mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(labelsView.mas_bottom).mas_offset(-19);
        }];
    }else{
        [followsButt mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(labelsView.mas_bottom).mas_offset(12);
        }];
    }
    if([info.isfollow intValue] == 1){
        followsButt.selected = YES;
        [followsButt setTitle:@"已关注" forState:UIControlStateNormal];
        [followsButt setTitleColor:TextTipColor forState:UIControlStateNormal];
        followsButt.backgroundColor = COLOR_ALPH(232,228,244,1);
        [followsButt setImage:KImageNamed(@"") forState:UIControlStateNormal];
    }else{
        followsButt.selected = NO;
        [followsButt setTitle:@" 关注" forState:UIControlStateNormal];
        [followsButt setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        followsButt.backgroundColor = MainColor;
        [followsButt setImage:KImageNamed(@"icon_personal_add_little") forState:UIControlStateNormal];
    }
    followsButt.hidden = NO;
    if(info.motto.length == 0){
        info.motto = @"Ta还没有添加个性签名！";
    }
    
    
    displayInput.hidden = YES;
    displayView.hidden = NO;
    NSMutableAttributedString * moreAttr = [[NSMutableAttributedString alloc] initWithString:info.motto attributes:@{NSForegroundColorAttributeName : [UIColor whiteColor], NSFontAttributeName : Font(14)}];
    KWeakSelf
    if(self.isOpenIntro){
        NSMutableAttributedString * closeAttr = [[NSMutableAttributedString alloc] initWithString:@".... 收起" attributes:@{NSForegroundColorAttributeName : [UIColor whiteColor], NSFontAttributeName : Font(14)}];
        [closeAttr setTextHighlightRange:NSMakeRange(0, closeAttr.length) color:[UIColor whiteColor] backgroundColor:UIColor.clearColor tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
            //点击
            weakSelf.isOpenIntro = NO;
            weakSelf.myOtherMessegeHeaderBlock(0);
        }];
        [moreAttr appendAttributedString:closeAttr];
        
        displayView.numberOfLines = 0;
        self.introHeight = [moreAttr boundingRectWithSize:CGSizeMake(KScreen_W-32, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading context:nil].size.height;
        
    }else{
        displayView.numberOfLines = 2;
        self.introHeight = [moreAttr boundingRectWithSize:CGSizeMake(KScreen_W-32, displayView.font.lineHeight*2) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading context:nil].size.height+2;
    }
    displayView.attributedText = moreAttr;
   
    if(self.introHeight < 20){
        self.introHeight = 20;
    }
    [displayView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_offset(weakSelf.introHeight);
    }];
    
    
}
#pragma mark -
- (void)likeTap:(UIPanGestureRecognizer*)sender {
    self.myOtherMessegeHeaderBlock(sender.view.tag);
}
- (void)followTap:(UIButton *)sender{
    self.myOtherMessegeHeaderBlock(4);
}
#pragma mark TLDisplayViewDelegate
- (void)displayView:(TLDisplayView *)label closeHeight:(CGFloat)height {

}

- (void)displayView:(TLDisplayView *)label openHeight:(CGFloat)height {

}
@end
